package com.imti.mall.order.controller;

import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.order.pojo.OmsOrder;
import com.imti.mall.model.order.pojo.OmsOrderOperateHistory;
import com.imti.mall.model.order.query.OmsOrderDelivery;
import com.imti.mall.model.order.query.OmsOrderQuery;
import com.imti.mall.model.order.vo.OmsOrderAndItem;
import com.imti.mall.model.order.vo.OmsOrderDetail;
import com.imti.mall.model.order.vo.OmsOrderMoneyInfo;
import com.imti.mall.model.order.vo.OmsOrderReceiverInfo;
import com.imti.mall.order.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "订单管理")
@RequestMapping("/api/v1/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private OmsOrderReturnApplyService omsOrderReturnApplyService;
    @Autowired
    private OmsCompanyAddressService omsCompanyAddressService;
    @Autowired
    private OrderOperateHistoryService orderOperateHistoryService;

  @ApiOperation(value = "查询订单")
  @GetMapping
  public CommonResult<CommonPage<OmsOrder>> list(OmsOrderQuery omsOrderQuery,
                                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

    List<OmsOrder> orderList = orderService.list(omsOrderQuery, pageSize, pageNum);
    return CommonResult.success(CommonPage.restPage(orderList));
  }

    @ApiOperation(value = "根据memberId查询订单以及该订单中的商品(memberId 1 2 3 4 5)APP端")
    @GetMapping(value = "getorderAndItemById/{memberId}")
    public CommonResult<CommonPage<OmsOrderAndItem>> getListById(OmsOrderQuery omsOrderQuery,
                                                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<OmsOrderAndItem> orderAndItem = orderService.findOrderAndItemById(omsOrderQuery, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(orderAndItem));
    }

    @ApiOperation("根据ID获取订单详情")
    @GetMapping(value = "/{orderId}")
    public CommonResult<OmsOrderDetail> detail(@PathVariable Long orderId) {
        OmsOrderDetail orderDetail = orderService.detail(orderId);
        return CommonResult.success(orderDetail);
    }
  @ApiOperation("根据ID删除订单")
  @PostMapping(value = "/deleteOrder")
  public CommonResult deleteById(Long ids[]) {
    int result = 0;
    for (int i = 0; i < ids.length; i++) {
      result = orderService.deletebyId(ids[i]);
    }
    return CommonResult.success(result);
  }

  @ApiOperation("根据ID关闭订单")
  @PostMapping(value = "/closeOrder")
  public CommonResult closeOrderById(Long ids[], String note) {
    OmsOrder omsOrder = new OmsOrder();
    OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
    int result = 0;
    omsOrder.setNote(note);
    for (int i = 0; i < ids.length; i++) {
      omsOrder.setId(ids[i]);
      //关闭订单
      result = orderService.closeOrderById(omsOrder);
      omsOrderOperateHistory.setOrderId(ids[i]);
    }
    //订单记录表插入记录
    omsOrderOperateHistory.setNote("关闭订单：" + note);
    omsOrderOperateHistory.setOperateMan("后台管理员");
    omsOrderOperateHistory.setOrderStatus(5);
    orderOperateHistoryService.insertOrderOperateHistory(omsOrderOperateHistory);
    return CommonResult.success(result);
  }

  @ApiOperation("发货,插入快递公司以及单号,修改订单状态")
  @PostMapping(value = "/updateDelivery")
  public CommonResult updateOrderForDelivery(@RequestBody List<OmsOrderDelivery> list) {
    OmsOrderDelivery delivery = new OmsOrderDelivery();
    OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
    int result = 0;
    for (int i = 0; i < list.size(); i++) {
      delivery.setDeliveryCompany(list.get(i).getDeliveryCompany());
      delivery.setOrderId(list.get(i).getOrderId());
      delivery.setDeliverySn(list.get(i).getDeliverySn());
      //发货
      result = orderService.updateOrderForDelivery(delivery);
      omsOrderOperateHistory.setOrderId(list.get(i).getOrderId());
    }
    //订单记录表插入记录
    omsOrderOperateHistory.setNote("完成发货");
    omsOrderOperateHistory.setOperateMan("后台管理员");
    omsOrderOperateHistory.setOrderStatus(2);
    orderOperateHistoryService.insertOrderOperateHistory(omsOrderOperateHistory);
    return CommonResult.success(result);
  }

  @ApiOperation("根据ID备注订单")
  @PostMapping(value = "/noteOrder")
  public CommonResult noteOrder(OmsOrder omsOrder) {
    int result = 0;
    result = orderService.noteById(omsOrder);
    //订单记录表插入记录
    OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
    omsOrderOperateHistory.setOrderId(omsOrder.getId());
    omsOrderOperateHistory.setNote("备注订单:" + omsOrder.getNote());
    omsOrderOperateHistory.setOperateMan("后台管理员");
    omsOrderOperateHistory.setOrderStatus(omsOrder.getStatus());
    orderOperateHistoryService.insertOrderOperateHistory(omsOrderOperateHistory);
    return CommonResult.success(result);
  }

  @ApiOperation("根据订单ID修改收货信息")
  @PostMapping(value = "/receiverInfo")
  public CommonResult receiverInfo(@RequestBody OmsOrderReceiverInfo omsOrderReceiverInfo) {
    int result = 0;
    result = orderService.updateReceiverInfo(omsOrderReceiverInfo);

    //订单记录表插入记录
    OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
    omsOrderOperateHistory.setOrderId(omsOrderReceiverInfo.getOrderId());
    omsOrderOperateHistory.setNote("修改收货信息");
    omsOrderOperateHistory.setOperateMan("后台管理员");
    omsOrderOperateHistory.setOrderStatus(omsOrderReceiverInfo.getStatus());
    orderOperateHistoryService.insertOrderOperateHistory(omsOrderOperateHistory);
    return CommonResult.success(result);
  }

  @ApiOperation("根据订单ID修改订单金额")
  @PostMapping(value = "/updateMoney")
  public CommonResult updateMoney(@RequestBody OmsOrderMoneyInfo omsOrderMoneyInfo) {
    int result = 0;
    result = orderService.updateOrderMoney(omsOrderMoneyInfo);

    //订单记录表插入记录
    OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
    omsOrderOperateHistory.setOrderId(omsOrderMoneyInfo.getOrderId());
    omsOrderOperateHistory.setNote("修改费用信息");
    omsOrderOperateHistory.setOperateMan("后台管理员");
    omsOrderOperateHistory.setOrderStatus(omsOrderMoneyInfo.getStatus());
    orderOperateHistoryService.insertOrderOperateHistory(omsOrderOperateHistory);
    return CommonResult.success(result);
  }
}
